package com.bangdream.michelia.model;

import com.alibaba.fastjson.JSON;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.BaseEntity;
import com.bangdream.michelia.tool.retrofithttp.RetroFactory;
import com.bangdream.michelia.tool.retrofithttp.TokenManager;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;
import com.bangdream.michelia.utils.L;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @date on 2018/8/1 20:55
 * @email 909732184@qq.com
 */
public class ICoursesModelImpl implements CoursesContract.ICoursesModel{
    @Override
        public Observable getCoursesList(Map<Object, Object> paramsMap) {

        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getCoursesList(stTokent,requestBody);
    }

    @Override
    public Observable getTaskCoursesList(Map<Object, Object> paramsMap) {

        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getList(stTokent,requestBody);

    }

    @Override
    public Observable getCourseInfo(Map<Object, Object> paramsMap) {
        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().getCourseInfo(AppCurrentUser.getInstance().getToken(),requestBody);
    }

    @Override
    public Observable getTeacherCoursesList(Map<Object, Object> paramsMap) {
        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getTeacherCoursesList(stTokent,requestBody);
    }

    @Override
    public Observable getCoursesDetails(String id) {
        return RetroFactory.getInstance().getCoursesDetails(id,AppCurrentUser.getInstance().getToken());
    }



    @Override
    public Observable getCoursesTypeList() {
        return RetroFactory.getInstance().getCoursesTypeList(AppCurrentUser.getInstance().getToken());
    }

    @Override
    public Observable getCommentList(Map<Object, Object> paramsMap) {
        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getCommentList(stTokent,requestBody);
    }

    @Override
    public Observable actionComment(Map<Object, Object> paramsMap) {
        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().actionComment(stTokent,requestBody);
    }

    @Override
    public Observable getVideoMsg(String id) {
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getVideoMsg(id,stTokent);
    }

    @Override
    public Observable getLearning(Map<Object, Object> paramsMap) {
        String jsonString =JSON.toJSONString(paramsMap);
        L.d(jsonString);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getLearnList(stTokent,requestBody);

    }

    @Override
    public Observable getCertList(Map<Object, Object> map) {
        String jsonString =JSON.toJSONString(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
           String stTokent = AppCurrentUser.getInstance().getToken();

        return RetroFactory.getInstance().getCertList(stTokent,requestBody);
//       return TokenManagerDef.work(() -> {
//           String stTokent = AppCurrentUser.getInstance().getToken();
//           return RetroFactory.getInstance().getCertList(stTokent,requestBody);
//       }).compose(TokenManagerDef.newInstance().getTransformer())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread());

    }

    @Override
    public Observable getLearningstate() {
        Map<String,String> map=new HashMap<>();
        map.put("userId",AppCurrentUser.getInstance().getUserInfo().getId());
        String jsonString =JSON.toJSONString(map);
        L.d(jsonString);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getLearningstate(stTokent,requestBody);

    }


}
